package demo;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * @Author: WWQ
 * @Date: 2019/4/4 14:08
 * @Version 1.0
 */
public class StreamAPITest3 {

    //2. 中间操作
	/*
		映射
		map——接收 Lambda ， 将元素转换成其他形式或提取信息。接收一个函数作为参数，该函数会被应用到每个元素上，并将其映射成一个新的元素。
		flatMap——接收一个函数作为参数，将流中的每个值都换成另一个流，然后把所有流连接成一个流
	 */
    List<Employee> emps = Arrays.asList(
            new Employee("李四", 59, 6666.66),
            new Employee("张三", 18, 9999.99),
            new Employee("王五", 28, 3333.33),
            new Employee("赵六", 8, 7777.77),
            new Employee("赵六", 8, 7777.77),
            new Employee("赵六", 8, 7777.77),
            new Employee("田七", 38, 5555.55)
    );
    List<String> list = new ArrayList<String>(Arrays.asList("ab", "cd", "ef", "1fsd", "asdf", "asdg", "ab", "cd", "ef", "1fsd", "asdf", "asdg"));


    public static Stream<Character> filterCharacter(String str) {
        List<Character> list = new ArrayList<>();

        for (Character ch : str.toCharArray()) {
            list.add(ch);
        }

        return list.stream();
    }

    @Test
    public void test1() {
        Stream<String> stream = list.stream().map(String::toUpperCase);
        stream.forEach(System.out::println);
    }

    @Test
    public void test2() {
        Stream<String> stream = emps.stream().map((e) -> e.getName());
        stream.forEach(System.out::println);
    }

    @Test
    public void test3() {
        Stream<Stream<Character>> stream = list.stream().map(StreamAPITest3::filterCharacter);
        stream.forEach((x) -> x.forEach(System.out::println));
    }

    @Test
    public void test4() {
        Stream<Character> stream = list.stream().flatMap(StreamAPITest3::filterCharacter);
        stream.forEach(System.out::println);

    }
}
